package it.asvp.asvp_portal.web.controller;

import it.asvp.asvp_portal.asvp_util.enumerative.ReceiverOption;
import it.asvp.asvp_portal.asvp_util.util.Constants;
import it.asvp.asvp_portal.data.entity.member.Member;
import it.asvp.asvp_portal.data.entity.notification.Notification;
import it.asvp.asvp_portal.data.entity.notification.NotificationReceiver;
import it.asvp.asvp_portal.service.services.MemberService;
import it.asvp.asvp_portal.service.services.NotificationService;
import it.asvp.asvp_portal.web.bean.NotificationBean;

import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


@Controller
@RequestMapping("/notification")
public class NotificationController extends BaseController{
	
	private static final Log log = LogFactory.getLog(ConventionController.class);
	@Autowired
	NotificationService notificationService;
	@Autowired
	MemberService memberService;

	@RequestMapping(value = "/default", method = RequestMethod.GET)
	public ModelAndView getAll(final HttpServletRequest request, HttpServletResponse response) {
		
		log.info("Accesso alla pagina di tutte le notification");

        try{
			
        	ModelAndView mv = getModelAndView("notification/default");
		    mv.addObject(Constants.Notification.NOTIFICATIONS, notificationService.getAllNotifications());
		    return mv;
		    
	    }catch (Exception e) {
		   return getErrorModelAndView(e);
	    }
	}
	
	@RequestMapping(value = "/insert", method = RequestMethod.GET)
	public ModelAndView insert(@ModelAttribute("NotificationBean")NotificationBean notificationBean) {

		log.info("Accesso alla form di inserimento di una nuova notifica");
		
	    try{
	    	
	       ModelAndView mv = getModelAndView("/notificazione/insert");
		   mv.addObject(Constants.Notification.NOTIFICATION_OPTIONS,notificationBean.getReceiverOptions() );
		   mv.addObject(Constants.Notification.NOTIFICATION_RECEIVERS,memberService.getAllMembers());
		   return mv;	
		   
	    }catch (Exception e) {
		   return getErrorModelAndView(e);
	    }
	}
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)
    public ModelAndView save(@ModelAttribute("NotificationBean")NotificationBean notificationBean) throws Exception{
		
        //log
		try{
			
            Notification notification=notificationBean.getNotification();
            //validazione
            ReceiverOption receiverOption=notification.getReceiverOption();
            
            if( ReceiverOption.ALCUNI_SOCI.equals(receiverOption)){
            	
            	Set<NotificationReceiver> notificationReceiverSet=new HashSet<NotificationReceiver>();
            	Set<Long> receivers=notificationBean.getReceivers();
            	
            	if(receivers==null || receivers.isEmpty()) 
            		         throw new IllegalArgumentException("Deve selezionare almeno un destinatario per questa opzione");
            		
            		for(Long idMember:receivers){
            			NotificationReceiver notificationReceiver=new NotificationReceiver();
            			Member member=new Member();
            			member.setId(idMember);
            			notificationReceiver.setMember(member);
            			notificationReceiver.setNotification(notification);
            			notificationReceiverSet.add(notificationReceiver);
            		}
            	
            	 notification.setNotificationReceivers(notificationReceiverSet);
            }
            
            notificationService.createNotification(getUserDetail(), notification);
         
            return new ModelAndView(Constants.REDIRECT+"/notification/default");
            
       }catch (Exception e) {
		   return getErrorModelAndView(e);
	   }
    }
}
